package com.tc.quartz.frame.base;

import com.github.pagehelper.PageInfo;
import com.tc.report.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 公用Service基类
 * @author yongjin.ye
 *
 */
public abstract class BaseService implements IBaseService {
	
	protected Logger log = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 子类设置此Dao
	 * @return
	 */
	@Override
	public abstract IBaseDao getBaseDao();
	
	/**
	 * 插入一个对象
	 * @param object
	 * @return
	 */
	@Override
	public int insert(Object object) {
		return this.getBaseDao().insert(object);
	}

	
	/**
	 * 更新一个对象
	 * @param object
	 * @return
	 */
	@Override
	public int update(Object object) {
		return this.getBaseDao().update(object);
	}
	/**
	 * 删除一个对象
	 * @param object
	 * @return
	 */
	@Override
	public int delete(Object object) {
		return this.getBaseDao().delete(object);
	}
	/**
	 * 选择对象
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getById(Object object) {
		return (T) this.getBaseDao().getById(object);
	}
	

	/**
	 * 统计总数
	 * @param object
	 * @return
	 */
	@Override
	public int getCount(Object object) {
		return this.getBaseDao().getCount(object);
	}

	/**
	 * 普通查询
	 * @param <T>
	 * @param object
	 * @return
	 */
	@Override
	public <T> List<T>  select(Object object){
		return this.getBaseDao().select(object);
	}
	
	/**
	 * 分页查询 
	 */
	@Override
	public <T> PageInfo<T> getPageList(Object obj, int pageNum, int pageSize) {
		return getPageList(obj, pageNum, pageSize, null);
	}
	
	/**
	 * 分页查询
	 */
	@Override
	public <T> PageInfo<T> getPageList(Object obj, int pageNum, int pageSize, String orderBy) {
		//注入排序字段
		if (StringUtils.isNotBlank(orderBy)) {
			try {
				BeanUtils.setProperty(obj, "__orderBy", orderBy.replaceAll("\\'", "''"));
			} catch (Exception e) {}
		}
		List<T> list = this.getBaseDao().select(obj, new RowBounds(pageNum,  pageSize));
		return new PageInfo<T>(list);
	}

}
